package day1130book;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeSet;

import day1130.StudentInfo;

public class Test {
	static Scanner sc = new Scanner(System.in);
	ArrayList<Book> list1 = new ArrayList<>();
	ArrayList<Bookcard> list2 = new ArrayList<>();

	public static void main(String[] args) {
		new Test().start();
	}
//获取一个字符串类型的时间。。.
	public String getTime() {
		Date date = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = ft.format(date);
		return str;
	}

	public void start() {
		while (true) {
			System.out.println("---请选择功能----");
			System.out.println("----1-图书馆购入新书--");
			System.out.println("----2-借书--");
			System.out.println("----3-查询某书的借出记录--");
			System.out.println("----4-查询借书卡的借出记录--");
			System.out.println("----5-显示图书列表--");
			System.out.println("----6-还书--");
			System.out.println("----7-退出系统--");
			System.out.println("----8-办理借书卡--");
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				addbook();
				break;
			case 2:
				borrowbook();
				break;
			case 3:
				finddata();
				break;
			case 4:
				findAllData();
				break;
			case 5:
				showInfo();
				break;
			case 6:
				bookBack();
				break;
			case 7:
				return;
			case 8:
				creatCard();
				break;
			default:
				System.out.println("输入指令有误!!!");
			}
		}

	}
	
//创办借书卡
	public void creatCard() {
		System.out.println("设置您的借书卡卡号");
		int id;
		do {
			id = sc.nextInt();
			sc.nextLine();
		} while (isExist1(id));
		Bookcard b =new Bookcard();
		b.setId(id);
		new BookcardDao().insert(b);
		System.out.println("办理成功");
	}
	public boolean isExist1(int id) {
		ArrayList<Bookcard> list = new BookcardDao().selectId(id);
		if(!list.isEmpty()) {
			System.out.println("卡号已经存在");
			return true;
		}else {
			return false;
		}
	}
	
//还书
	public void bookBack() {
		System.out.println("请输入要还的书名");
		String name = sc.nextLine();
		Book b = new Book();b.setName(name);
		int res = new BookDao().update1(b);
		if(res!=0)System.out.println("还书成功");
		else System.out.println("书名输入有误");
	}
	
//展示全部图书信息
	public void showInfo() {
		Book b = new Book();
		ArrayList<Book> list = new BookDao().select(b);
		for(Book k : list)
			System.out.println(k.toString());
	}
	
//查找某本书的借阅信息
	public void finddata() {
		System.out.println("请输入要查找的书籍名字");
		String name = sc.nextLine();
		Book b = new Book();b.setName(name);
		ArrayList<Book> list = new BookDao().select(b);
		if (list.isEmpty())
			System.out.println("没有这本书的借出记录");
		else System.out.println(list.get(0).toString());
	}
	
//查找借书卡的借书记录 
	public void findAllData() {
		System.out.println("请输入要查询的借书卡卡号");
		int id = sc.nextInt();sc.nextLine();
		RecordModel rm = new RecordModel();rm.setId(id);
		ArrayList<RecordModel> list = new RecordDao().select(rm);
		if(!list.isEmpty()) {
			for(RecordModel md : list)
				System.out.println(md.toString());
		}
		else
			System.out.println("该卡号暂时没有借阅记录");
		
	}
	
//借书
	public void borrowbook() {
		System.out.println("现在可提供借阅的书有：");
		Book b = new Book();b.setStatus("on");
		ArrayList<Book> list = new BookDao().select(b);
		for(Book bb : list) {
			System.out.println(bb.toString());
		}
		System.out.println("请输入您的借书卡卡号：");
		int id = sc.nextInt();sc.nextLine();
		ArrayList<Bookcard> list1 = new BookcardDao().selectId(id);
		if(list1.isEmpty()) {
			System.out.println("不存在该卡号！");
			return ;
		}
		System.out.println("请输入您要借的书名");
		String name = sc.nextLine();
		Book bbb = new Book();bbb.setName(name);
		int res = new BookDao().update(bbb);
		if(res!=0) {
			String time = getTime();
			RecordModel record = new RecordModel();
			record.setId(id);record.setName(name);record.setTime(time);
			new RecordDao().insert(record);
			System.out.println("借阅成功");
		}else {
			System.out.println("输入有误");
		}
	}

	//图书馆购入新书
	public void addbook() {
		while (true) {
			input();
			System.out.println("是否继续录入？y/n ");
			if (!"y".equalsIgnoreCase(sc.nextLine()))
				break;
		}
	}
	public void input() {
		System.out.println("请输入进入书籍的名字");
		String name = new String();
		do {
			name = sc.nextLine();
		} while (isExist(name));
		Book b = new Book();
		b.setCount(0);
		b.setName(name);
		b.setStatus("on");
		new BookDao().insert(b);
	}
	public boolean isExist(String name) {
		Book b  = new Book(); b.setName(name);
		ArrayList<Book> list = new BookDao().select(b);
		if(!list.isEmpty()) {
			System.out.println("书籍已经存在");
			return true;
		}else {
			return false;
		}
	}

}
